package Lee_code;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @author blh
 * @date 2024年07月18日 9:18
 */
public class Lee_128_718 {
    public int longestConsecutive(int[] nums) {
        int len = nums.length;
        HashSet<Integer> hashSet = new HashSet<>();
        for (int num : nums) {
            hashSet.add(num);
        }
        int maxLen = 0;
        for (int num : nums) {
            if (hashSet.contains(num + 1)) {
                maxLen = Math.max(maxLen, 1);
            } else {
                int temp = 1;
                while (hashSet.contains(num-1)){
                    temp++;
                    num--;
                }
                maxLen = Math.max(temp, maxLen);
            }
        }
        return maxLen;
    }
}
